import { action, observable } from 'mobx';
import { message } from 'antd';
import * as API from "../../apis/performance/accessItemListApi";

/**
 * 绩效-考核设置-考核指标项
 * jiansong 2019/6/11
 */
export default class AccessItemListStore {

    @observable loading = true; //加载中
    @observable canedit = false; // 是否能编辑
    @observable showUpdataDialog = false; //是否展示新增或编辑对话框
    @observable showShare = false; //是否展示共享范围对话框
    @observable data = []; //表单数据
    @observable shareData = []; //共享列表数据
    @observable formulaData = [];//自定义公式的值

    //指标类型
    itemtype = {
        '1': '定性',
        '2': '定量',
    }
    //状态类型
    isvalid = {
        '1': '是',
        '0': '否',
    }
    typeOption = [
        { key: "1", showname: "定性" },
        { key: "2", showname: "定量" }
    ];
    formulaOption = [
        { key: "1", showname: "公式1[完成值/目标值*5]" },
        { key: "5", showname: "公式5[目标值/完成值*3.5]" },
        { key: "11", showname: "公式[完成值/目标值*最大分制]" },
        { key: "12", showname: "自定义公式" },
        { key: "13", showname: "自定义Java类" },
    ];

    @observable form = {
        operation: "add", //操作类型
        itemname: "", //指标名称
        itemdesc: "", //指标描述
        itemtype: "1", //指标类型
        itemunit: "", //数值单位
        formula: "1", //计算公式
        javaclass: "", //java类
        isvalid: "1", //是否启用
        kpiformula: "", //自定义公式的值
    };

    @observable itemid = ''//考核指标id

    @action("渲染table数据")
    getTableDatas = (params) => {
        this.loading = true;
        API.accessItemListApi(params).then(action(res => {
            if (res.api_status) { // 接口请求成功/失败处理
                this.data = res.datas; // table 请求数据
                this.canedit = res.canedit;
            } else {
                message.error(res.msg || '接口调用失败！')
            }
            this.loading = false;
        }));
    };

    @action("表单关闭的时候清空表单内容")
    closeForm = () => {
        this.initForm();
        this.changeShowUpdataDialog();
    };

    @action("搜索条件改变")
    onFormChange = (params) => {
        for (let key in params) {
            this.form[key] = params[key];
        }
    }

    @action("初始化添加表单")
    ininAddForm = () => {
        this.changeShowUpdataDialog();
    };

    @action('显示共享范围dialog')
    showShowShare = (id) => {
        this.itemid = id;
        if (this.itemid) {
            //获取考核指标项的共享范围列表
            this.getShareList();
        }
        this.showShare = true;
    }

    @action("关闭共享范围dialog")
    closeShowShare = () => {
        this.shareData = {};
        this.showShare = false;
    }

    @action("获取考核指标项共享列表")
    getShareList = () => {
        API.accessItemShareListApi({ itemid: this.itemid }).then(action(res => {
            if (res.api_status) {
                this.shareData = res.datas;
            } else {
                message.error(res.msg || '接口调用失败！')
            }
        }));
    }

    @action("切换编辑框是否展示")
    changeShowUpdataDialog = () => {
        this.showUpdataDialog = !this.showUpdataDialog;
    };

    @action("初始化表单内容")
    initForm = () => {
        this.form = {
            operation: "add",
            itemname: "", //指标名称
            itemdesc: "", //指标描述
            itemtype: "1", //指标类型
            itemunit: "", //数值单位
            formula: "1", //计算公式
            javaclass: "", //java类
            isvalid: "1", //是否启用
            kpiformula: "", //自定义公式的值
        };
        this.formulaData = [];
    };

    @action("初始化编辑表单内容")
    initEditForm = (params) => {
        this.form = {
            operation: "edit",
            id: params.id,
            itemname: params.itemname, //指标名称
            itemdesc: params.itemdesc, //指标描述
            itemtype: params.itemtype, //指标类型
            itemunit: params.itemunit, //数值单位
            formula: params.formula, //计算公式
            isvalid: params.isvalid, //是否启用
            kpiformula: "", //自定义公式的值
        };

        this.formulaData = [];
        if (params.formula == '12') {
            let formulad = params.formuladetail
            this.form.kpiformula = formulad.replace("gval", "目标值").replace("cval", "完成值");

            //初始化拖拽div中的tag元素
            let reg = /[+\-*\/\(\)]{1}/ig;
            formulad = formulad == null ? "" : formulad.replace(reg, function (a, i) { return ',' + a + ','; }).replace(/,,/ig, ',');
            formulad = formulad.substring(0, formulad.length);
            let kpiFormuleAry = formulad.split(',');

            kpiFormuleAry.map((item) => {
                if (item == 'gval') {
                    this.formulaData.push({ "type": "target_value", "value": "目标值", no: Math.random() })
                } else if (item == 'cval') {
                    this.formulaData.push({ "type": "completion_value", "value": "完成值", no: Math.random() })
                } else if (parseFloat(item)) {
                    this.formulaData.push({ "type": "number", "value": item, no: Math.random() })
                } else if (item == '(') {
                    this.formulaData.push({ "type": "left_parenthesis", "value": item, no: Math.random() })
                } else if (item == ')') {
                    this.formulaData.push({ "type": "right_parenthesis", "value": item, no: Math.random() })
                } else if (item == '+') {
                    this.formulaData.push({ "type": "addition", "value": item, no: Math.random() })
                } else if (item == '-') {
                    this.formulaData.push({ "type": "substruction", "value": item, no: Math.random() })
                } else if (item == '/') {
                    this.formulaData.push({ "type": "division", "value": item, no: Math.random() })
                } else if (item == '*') {
                    this.formulaData.push({ "type": "multiplication", "value": item, no: Math.random() })
                }
            });
        } else if (params.formula == '13') {
            this.form.javaclass = params.formuladetail;
        }
        this.changeShowUpdataDialog();
    };

    @action("考核指标项-操作")
    operation = () => {
        this.loading = true;
        let params = { ...this.form };
        if (params.formula == '12') {
            params.kpiformula = (params.kpiformula).replace("目标值", "gval").replace("完成值", "cval");
        };

        API.accessItemOperationApi(params).then(action(res => {
            if (res.api_status) {
                if (!res.hasRight) {
                    message.error("对不起，您暂时没有权限！");
                } else {
                    message.success("操作成功");
                    this.changeShowUpdataDialog()
                    this.getTableDatas()
                    this.initForm()
                }
            } else {
                message.error(res.msg || '接口调用失败！')
            }
            this.formulaData = [];
            this.form.kpiformula = '';
            this.loading = false;
        }));
    };

    @action("考核指标项-共享范围-操作")
    operationShare = (data) => {
        API.accessItemShareOperationApi({
            ...data,
            itemid: this.itemid,
            type: 'add'
        }).then(action(res => {
            if (res.api_status) {
                if (!res.hasRight) {
                    message.error("对不起，您暂时没有权限！");
                } else {
                    this.getShareList();
                    message.success("操作成功");
                }
            } else {
                message.error(res.msg || '接口调用失败！')
            }
        }));
    };

    @action("考核指标项-共享范围-删除")
    delShare = (idList) => {
        API.accessItemShareOperationApi({
            type: 'del',
            ids: idList
        }).then(action(res => {
            if (res.api_status) {
                if (!res.hasRight) {
                    message.error("对不起，您暂时没有权限！");
                } else {
                    this.getShareList();
                    message.success("操作成功");
                }
            } else {
                message.error(res.msg || '接口调用失败！')
            }
        }));
    };

}